package com.itmk.web.ai.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itmk.HeHeXinXiApi.api.TextInUtil;
import com.itmk.utils.Result;
import com.itmk.web.companyInfo.entity.JobInfo;
import com.itmk.web.userInfo.entity.*;
import com.itmk.zhipuapi.demo.V4OkHttpClientTest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@ResponseBody
@RequestMapping("/api")
public class iicontroller {

    @RequestMapping("/searchImage")
    public Result meyh(MultipartFile resume) {
        //接收简历pdf
        //对pdf进行重命名并存储到项目中
        String filePDF = null;
        if(resume!=null){
            //file重命名
            String originalFilename = resume.getOriginalFilename();
            String ext = "."+originalFilename.split("\\.")[1];
            String imageUUID = UUID.randomUUID().toString().replace("-","");
            String fileName = imageUUID+ext;
            filePDF = fileName;
            //上传图片
//            ApplicationHome applicationHome = new ApplicationHome(this.getClass());
//            String pre = applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath()+
//                    "\\src\\main\\resources\\static\\images\\companyLogo\\";
            String property = System.getProperty("user.dir")+"\\itmk-base-web";
            String pre = property + "\\target\\classes\\static\\pdfs\\";

            String path1 = pre+fileName;
            String path = path1.replace("/","\\");
            try {
                resume.transferTo(new File(path));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        if(filePDF!=null){
            JSONObject a;
            try {
                //将pdfs名字传给函数
                a = TextInUtil.sendRequest(filePDF);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            JSONObject resultObject = a.getJSONObject("result");
            JSONArray jsonArray = resultObject.getJSONArray("lines");
            List<String> list = new ArrayList<>();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject lineObject = jsonArray.getJSONObject(i);
                String text = lineObject.getString("text");
                if(text.contains("：")){
                    String text2 = text.split("：")[1];
                    list.add(text2);
                }else{
                    list.add(text);
                }
                System.out.println("Text: " + text);
            }
            BasicInfo basicInfo = new BasicInfo();
            NewEducation educationInfo = new NewEducation();
            Undergo undergo = new Undergo();
            ForjobInfo forjobInfo = new ForjobInfo();

            int i= 0;
            for (String s : list) {
                System.out.println(i+":"+s);
                i++;
            }
            //设置basicInfo信息
            String name = list.get(1);
            basicInfo.setName(name);
            String age = list.get(2);
            basicInfo.setAge(age);
            String gender= list.get(3);
            basicInfo.setGender(gender);
            String post = list.get(4);
            basicInfo.setPost(post);
            String birthday = list.get(5);
            basicInfo.setBirthday(birthday);
            String location = list.get(6);
            basicInfo.setLocation(location);

            //设置educationInfo的信息
            String background = list.get(8);
            educationInfo.setBackground(background);
            String type= list.get(9);
            educationInfo.setType(type);
            String school = list.get(10);
            educationInfo.setSchool(school);
            String major = list.get(11);
            educationInfo.setMajor(major);
            String start = list.get(12);
            String end = list.get(13);
            undergo.setStart(start);
            undergo.setEnd(end);
            educationInfo.setUndergo(undergo);
            String experience = list.get(14);
            educationInfo.setExperience(experience);

            //设置ForJobInfo
            String identity = list.get(16);
            forjobInfo.setIdentity(identity);
            String firstJobTime = list.get(17);
            forjobInfo.setFirst_job_time(firstJobTime);
            String status = list.get(18);
            forjobInfo.setStatus(status);
            String city = list.get(19);
            forjobInfo.setCity(city);
            String position = list.get(20);
            forjobInfo.setPosition(position);
            String job_experience = list.get(21);
            forjobInfo.setJob_experience(job_experience);
            String advantage = list.get(22);
            forjobInfo.setAdvantage(advantage);

            UserInfo userInfo = new UserInfo();
            userInfo.setBasicInfo(basicInfo);
            userInfo.setEducationInfo(educationInfo);
            userInfo.setForjobInfo(forjobInfo);
            return Result.ok().put("data",userInfo);
        }
        return Result.error("图片传送失败");
    }

    @RequestMapping("/ai")
    @ResponseBody
    public JSONObject  test(String content) {
        String res = V4OkHttpClientTest.testAsyncInvoke(content);
        JSONObject jsonObject = JSONObject.parseObject(res);
//        JSONObject data = jsonObject.getJSONObject("data");
//        JSONArray choices = data.getJSONArray("choices");
//        String ResContent = null;
//        for (int i = 0; i < 1; i++) {
//            JSONObject message = choices.getJSONObject(i);
//            ResContent = message.getString("content");
//        }
//       if(ResContent==null){
//           return "没读取到内容；";
//       }
//       return ResContent;
//    }
        return jsonObject;
    }
}
